import { createI18n } from 'vue-i18n'
import { useSysGlobalStore } from '@/stores/global'
import { tool } from '@/utils/tool'
import type { Pinia } from 'pinia'

export const i18n = (pinia: Pinia) => {
    const sysGlobalStore = useSysGlobalStore(pinia)

    return createI18n({
        legacy: false,
        locale: sysGlobalStore.sysLang,
        fallbackLocale: 'zh-cn',
        globalInjection: true,
        messages: sysGlobalStore.sysLangMessages
    })
}

export default i18n

// el语言格式转化
export const formatLang = (locale: string) => {
    let esLang = ''
    if (['zh-cn', 'nb-no', 'zh-tw', 'pt-br', 'ug-cn', 'hy-am'].includes(locale)) {
        esLang = tool.toCamelCase(locale.toLowerCase())
    } else {
        esLang = locale.slice(0, 2)
    }

    return esLang
}
